package net.jumpengine.bean;

import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import net.jumpengine.M;

/**
  * 多条线段的base类,各种详细参数见构造函数
 * <br /><br />
 * <b>作者:&nbsp;</b>张天文 <br />
 * <b>日期:&nbsp;</b>2020年9月3日 下午3:44:12<br />
 * @date
 */
public class LinesBase extends Base {
	/** 线段数据,二维是线段的数据,按顺序分别是x1,y1,x2,y2 */
	protected double[][] lines;
	protected Color color;
	protected int lineWidth=1;
	
	public LinesBase(int xSpace,int ySpace,Color color) {
		this(xSpace, ySpace, color, 1, 0);
	}
	/**
	 * 以一定间隔绘制线段, 分为x(垂直)方向间隔和y(水平)方向间隔
	 * @param xSpace x(垂直)方向间隔, 小于等于0不绘制
	 * @param ySpace y(水平)方向间隔, 小于等于0不绘制
	 * @param color 线条颜色
	 * @param lineWidth 线条宽度
	 * @param index 层级
	 */
	public LinesBase(int xSpace,int ySpace,Color color,int lineWidth,int index) {
		this(0, 0, M.w(), M.h(), xSpace, ySpace, color, lineWidth, index);
	}
	
	/**
	 * 以一定间隔绘制线段, 分为x(垂直)方向间隔和y(水平)方向间隔
	 * @param x 坐标
	 * @param y 坐标
	 * @param width 宽度
	 * @param height 高度
	 * @param xSpace x(垂直)方向间隔, 小于等于0不绘制
	 * @param ySpace y(水平)方向间隔, 小于等于0不绘制
	 * @param color 线条颜色
	 * @param lineWidth 线条宽度
	 * @param index 层级
	 */
	public LinesBase(int x,int y,int width,int height,int xSpace,int ySpace,Color color,int lineWidth,int index) {
		super(x,y,width,height,index);
		this.color=color;
		this.lineWidth=lineWidth;
		setSpace(xSpace, ySpace);
	}
	
	public LinesBase(double xScale,double yScale,Color color) {
		this(xScale, yScale, color, 1, 0);
	}
	/**
	  * 以游戏尺寸一定比例作为间隔绘制线段, 分为x(垂直)方向间隔和y(水平)方向间隔
	 * @param xScale x(垂直)方向相与宽度的比值, 小于等于0不绘制
	 * @param yScale y(水平)方向相与高度的比值, 小于等于0不绘制
	 * @param color 线条颜色
	 * @param lineWidth 线条宽度
	 * @param index 层级
	 */
	public LinesBase(double xScale,double yScale,Color color,int lineWidth,int index) {
		this(0, 0, M.w(), M.h(), xScale, yScale, color, lineWidth, index);
	}
	
	public LinesBase(int x,int y,int width,int height,double xScale,double yScale,Color color,int lineWidth,int index) {
		super(x,y,width,height,index);
		this.color=color;
		this.lineWidth=lineWidth;
		setSpace(xScale, yScale);
	}
	
	public LinesBase(double[][] dates,Color color) {
		this(dates, color, 1, 0);
	}
	
	/**
	 * 以直线公式或直接两点坐标绘制一批线段
	 * @param dates 二维数组长度2: y=ax+b中的a和b两值;长度4: a,b,x1,x2四值;长度5: x1,y1,x2,y2,无用标志,因为和前一个值数目相等,添加一位标志
	 * @param color 线条颜色
	 * @param lineWidth 线条宽度
	 * @param index 层级
	 */
	public LinesBase(double[][] dates,Color color,int lineWidth,int index) {
		this(0, 0, M.w(), M.h(),dates,color,lineWidth,index);
	}
	
	public LinesBase(int x,int y,int width,int height,double[][] dates,Color color,int lineWidth,int index) {
		super(x,y,width,height,index);
		this.color=color;
		this.lineWidth=lineWidth;
		setSpace(dates);
	}
	
	@Override
	public void draw(GraphicsContext g) {
		if(lines==null)return;
		g.setLineWidth(lineWidth);
		g.setStroke(color);
		for(double[] dbl:lines) {
			g.strokeLine(dbl[0], dbl[1],dbl[2],dbl[3]);
		}
	}
	
	/**
	 * @param xSpace
	 * @param ySpace
	 * @see #LinesBase(int, int, Color, int, int )
	 */
	public void setSpace(int xSpace,int ySpace) {
		int num=width/xSpace+height/ySpace+2;
		if(num<=0)return;
		//M.l().debug(this.getClass(), "xSpace:",xSpace," ySpace:",ySpace," width/xSpace:",width/xSpace," height/ySpace:",height/ySpace," num:",num);
		lines=new double[num][4];
		int tmp=0;
		if(xSpace>0) {
			for(int i=x;i<=x+width;i+=xSpace) {
				lines[tmp][0]=i;
				lines[tmp][1]=y;
				lines[tmp][2]=i;
				lines[tmp][3]=y+height;
				tmp++;
			}
		}
		
		if(ySpace<=0)return;
		for(int i=y;i<=y+height;i+=ySpace) {
			lines[tmp][0]=x;
			lines[tmp][1]=i;
			lines[tmp][2]=x+width;
			lines[tmp][3]=i;
			tmp++;
		}
	}
	
	/**
	 * 参见 LinesBase(double xScale,double yScale,Color color,int lineWidth,int index)
	 * @param xScale
	 * @param yScale
	 * @see # LinesBase(double,double,Color,int,int)
	 */
	public void setSpace(double xScale,double yScale) {
		double unit;
		int xCount=0,yCount=0,num=(int)(xScale<=0?0:1/xScale+1)+(int)(yScale<=0?0:1/yScale+1);
		if(num<=0)return;
		lines=new double[num][4];
		if(xScale>0) {
			unit=xScale*width;
			xCount=(int) (width/unit);
			for(int i=0;i<=xCount;i++) {
				lines[i][0]=x+i*unit;
				lines[i][1]=y;
				lines[i][2]=x+i*unit;
				lines[i][3]=y+height;
			}
		}
		if(yScale>0) {
			unit=yScale*height;
			yCount=(int) (height/unit);
			for(int i=0;i<=yCount;i++) {
				lines[i+xCount][0]=x;
				lines[i+xCount][1]=y+i*unit;
				lines[i+xCount][2]=x+width;
				lines[i+xCount][3]=y+i*unit;
			}
		}
	}
	
	/**
	 * 参见 LinesBase(double[][] dates,Color color,int lineWidth,int index)
	 * @param dates
	 * @see #LinesBase(double[][],Color,int,int)
	 */
	public void setSpace(double[][] dates) {
		if(dates==null)return;
		lines=new double[dates.length][4];
		int len,ind=0;
		int w=width,h=height;
		for(double[] dbl:dates) {
			len=dbl.length;
			if(len==5) {
				lines[ind][0]=dbl[0];
				lines[ind][1]=dbl[1];
				lines[ind][2]=dbl[2];
				lines[ind][3]=dbl[3];
			}else {
				if(len==2) {
					lines[ind][0]=x;	
					lines[ind][2]=x+w;	
				}else {
					lines[ind][0]=dbl[2];
					lines[ind][2]=dbl[3];
				}
				lines[ind][1]=dbl[0]*lines[ind][0]+dbl[1];
				lines[ind][3]=dbl[0]*lines[ind][2]+dbl[1];
				
				if(lines[ind][1]>h || lines[ind][3]<0) {
					lines[ind][1]=y;
					lines[ind][3]=y+h;
					lines[ind][0]=lines[ind][1]/dbl[0]-dbl[1];
					lines[ind][2]=lines[ind][3]/dbl[0]-dbl[1];
				}

			}
			ind++;
		}
	}
	
	public double[][] getLines() {
		return lines;
	}
	public void setLines(double[][] lines) {
		this.lines = lines;
	}
	public Color getColor() {
		return color;
	}
	public void setColor(Color color) {
		this.color = color;
	}
	public int getLineWidth() {
		return lineWidth;
	}
	public void setLineWidth(int lineWidth) {
		this.lineWidth = lineWidth;
	}
	
	public String toDataString() {
		if(lines==null)return "{net.jumpengine.bean.LinesBase}:lines is null";
		StringBuilder sbr=new StringBuilder("{net.jumpengine.bean.LinesBaseLinesBase}:");
		boolean first=true;
		String lineSeparator = java.security.AccessController.doPrivileged(
	            new sun.security.action.GetPropertyAction("line.separator"));
		sbr.append(lineSeparator);
		int count=0;
		for(double[] date:lines) {
			sbr.append('[');
			for(double dbl:date) {
				if(!first)sbr.append(',');
				first=false;
				sbr.append(dbl);
			}
			sbr.append("],");
			first=true;
			count++;
			if((count+1) % 4==1)sbr.append(lineSeparator);
		}
		sbr.append(lineSeparator);
		return sbr.toString();
	}
}
